<?php

namespace app\api\controller\xyvenue;

use app\common\controller\Api;
use app\api\model\xyvenue\Config;
use app\api\model\xyvenue\store\Store;
use app\api\model\xyvenue\recharge\Recharge;
use app\api\model\xyvenue\Notice;
use app\api\model\xyvenue\Level;
use think\Db;

/**
 * 通用接口
 */
class Common extends Api
{
	protected $noNeedLogin = ['init','index','recharge','level','ctrlMachine','ctrlRoom','ctrlAir','ctrlLight'];
	protected $noNeedRight = ['*'];


	/**
	 * 加载通用配置
	 */
	public function init()
	{
		 // 判断缓存
		 $cacheKey = 'common-init';
		 $data = cache($cacheKey);
 
		 if (!$data) {
			 $data = [
				 "appStyle" => Config::getValueByName('appstyle'),
				 "appConfig" => Config::getValueByName('xyvenue'),
				 "tabBarList"  => Config::getValueByName('tabbar'),
			 ];
			 cache($cacheKey, $data, 1000);
		 }
 
		 $this->success('init',$data);
	}
	
	public function getDoorStatus(){
	    
	     $params = $this->request->get();
	     
	       // 检查记录是否存在
        $existingRecord = Db::name('xyvenue_room')
                            ->where('room_id', $params['room_id'])
                            ->find();
                            
	     $this->success('设置成功',$existingRecord);
	}
	
	public function ctrlMachine(){
	    $params = $this->request->post('ctrlMachine/a');
	   // print_r($params);
	    
	    $user_id = $this->auth->id?$this->auth->id:1;
	    $params['user_id'] = $user_id;
	    
	     // 检查记录是否存在
        $existingRecord = Db::name('xyvenue_setting')
                            ->where('user_id', $user_id)
                            ->find();
    
        if ($existingRecord) {
            // 更新记录
            Db::name('xyvenue_setting')
              ->where('user_id', $user_id)
              ->update($params);
        } else {
            // 插入新记录
            Db::name('xyvenue_setting')->insert($params);
        }
            
	   $this->success('设置成功');
	}
	
	
	public function ctrlRoom(){
	    
	    $params = $this->request->post();
	    $tablename = 'xyvenue_room';
	    
	     // 检查记录是否存在
        $existingRecord = Db::name($tablename)
                            ->where('room_id', $params['room_id'])
                            ->find();
    
        if ($existingRecord) {
            // 更新记录
            Db::name($tablename)
              ->where('room_id', $params['room_id'])
              ->update($params);
        } else {
            // 插入新记录
            Db::name($tablename)->insert($params);
        }
            
	   $this->success('设置成功');
	}
	
	public function ctrlAir(){
	    
	    $params = $this->request->post();
	    
	   // print_r($params);
	    
	    $tablename = 'xyvenue_air';
	    
	     // 检查记录是否存在
        $existingRecord = Db::name($tablename)
                            ->where('machine_id', $params['machine_id'])
                            ->find();
    
        if ($existingRecord) {
            // 更新记录
            Db::name($tablename)
              ->where('machine_id', $params['machine_id'])
              ->update($params);
        } else {
            // 插入新记录
            Db::name($tablename)->insert($params);
        }
            
	   $this->success('设置成功');
	}
	
	
	public function ctrlLight(){
	    
	    $params = $this->request->post();
	    
	   // print_r($params);
	    
	    $tablename = 'xyvenue_light';
	    
	     // 检查记录是否存在
        $existingRecord = Db::name($tablename)
                            ->where('machine_id', $params['machine_id'])
                            ->find();
    
        if ($existingRecord) {
            // 更新记录
            Db::name($tablename)
              ->where('machine_id', $params['machine_id'])
              ->update($params);
        } else {
            // 插入新记录
            Db::name($tablename)->insert($params);
        }
            
	   $this->success('设置成功');
	}
	
	
	
	

	/**
	 * 加载首页数据
	 */
	public function index($store_id = 0){

		// 场馆信息
		$sroreInfo = Store::getDetail($store_id);

		// 充值套餐
		$rechargeList = Recharge::getList(['limit'=>4]);

		// 最新公告
		$noticeList = Notice::getList(['limit'=>3]);

		$data = [
			'storeInfo' 	=> $sroreInfo,
			'rechargeList'  => $rechargeList,
			'noticeList'	=> $noticeList,
		];
        $this->success('首页数据', $data);
	}

	/**
	 * 充值套餐
	 */
	public function recharge(){
		$params = $this->request->get();
        $data = Recharge::getList($params);
        $this->success('充值套餐', $data);
	}

	/**
	 * 会员等级
	 */
	public function level(){
		$params = $this->request->get();
        $data = Level::getList($params);
        $this->success('会员等级', $data);
	}

	

}
